<!DOCTYPE html>
<!-- Ported from the OpenGL Samples Pack https://github.com/g-truc/ogl-samples/blob/master/tests/gl-330-sampler-filter.cpp -->
<html lang="en">

<head>
    <title>WebGL 2 Samples - sampler_filter</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <script src="utility.js"></script>
</head>

<body>
    <div id="info">WebGL 2 Samples - sampler_filter</div>
    <p id="description">
    This sample demonstrates different texture filtering options for min/mag filters (view source for details).
    </p>
    <script id="vs" type="x-shader/x-vertex">
        #version 300 es
        #define POSITION_LOCATION 0
        #define TEXCOORD_LOCATION 4

        precision highp float;
        precision highp int;

        uniform mat4 mvp;

        layout(location = POSITION_LOCATION) in vec2 position;
        layout(location = TEXCOORD_LOCATION) in vec2 textureCoordinates;

        out vec2 v_st;

        void main()
        {
            v_st = textureCoordinates;
            gl_Position = mvp * vec4(position, 0.0, 1.0) ;
        }
    </script>

    <script id="fs" type="x-shader/x-fragment">
        #version 300 es
        #define FRAG_COLOR_LOCATION 0

        precision highp float;
        precision highp int;

        uniform sampler2D diffuse;

        in vec2 v_st;

        layout(location = FRAG_COLOR_LOCATION) out vec4 color;

        void main()
        {
            color = texture(diffuse, v_st);
        }
    </script>

    <script>
    (function()  {
        'use strict';

        var canvas = document.createElement('canvas');
        canvas.width = Math.min(window.innerWidth, window.innerHeight);
        canvas.height = canvas.width;
        document.body.appendChild(canvas);

        var gl = canvas.getContext( 'webgl2', { antialias: false } );
        var isWebGL2 = !!gl;
        if(!isWebGL2) {
            document.getElementById('info').innerHTML = 'WebGL 2 is not available.  See <a href="https://www.khronos.org/webgl/wiki/Getting_a_WebGL_Implementation">How to get a WebGL 2 implementation</a>';
            return;
        }

        // -- Divide viewport

        var windowSize = {
            x: canvas.width,
            y: canvas.height
        };

        var Corners = {
            TOP_LEFT: 0,
            TOP_RIGHT: 1,
            BOTTOM_RIGHT: 2,
            BOTTOM_LEFT: 3,
            MAX: 4
        };

        var viewport = new Array(Corners.MAX);

        viewport[Corners.BOTTOM_LEFT] = {
            x: 0,
            y: 0,
            z: windowSize.x / 2,
            w: windowSize.y / 2
        };

        viewport[Corners.BOTTOM_RIGHT] = {
            x: windowSize.x / 2,
            y: 0,
            z: windowSize.x / 2,
            w: windowSize.y / 2
        };

        viewport[Corners.TOP_RIGHT] = {
            x: windowSize.x / 2,
            y: windowSize.y / 2,
            z: windowSize.x / 2,
            w: windowSize.y / 2
        };

        viewport[Corners.TOP_LEFT] = {
            x: 0,
            y: windowSize.y / 2,
            z: windowSize.x / 2,
            w: windowSize.y / 2
        };

        // -- Initialize program

        var program = createProgram(gl, getShaderSource('vs'), getShaderSource('fs'));

        var uniformMvpLocation = gl.getUniformLocation(program,"mvp");
        var uniformDiffuseLocation = gl.getUniformLocation(program,"diffuse");

        // -- Initialize buffer

        var positions = new Float32Array([
            -1.0, -1.0,
             1.0, -1.0,
             1.0,  1.0,
             1.0,  1.0,
            -1.0,  1.0,
            -1.0, -1.0
        ]);
        var vertexPosBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, vertexPosBuffer);
        gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW);
        gl.bindBuffer(gl.ARRAY_BUFFER, null);

        var texcoords = new Float32Array([
            0.0, 1.0,
            1.0, 1.0,
            1.0, 0.0,
            1.0, 0.0,
            0.0, 0.0,
            0.0, 1.0
        ]);
        var vertexTexBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, vertexTexBuffer);
        gl.bufferData(gl.ARRAY_BUFFER, texcoords, gl.STATIC_DRAW);
        gl.bindBuffer(gl.ARRAY_BUFFER, null);

        // -- Initialize vertex array

        var vertexArray = gl.createVertexArray();
        gl.bindVertexArray(vertexArray);

        var vertexPosLocation = 0; // set with GLSL layout qualifier
        gl.bindBuffer(gl.ARRAY_BUFFER, vertexPosBuffer);
        gl.vertexAttribPointer(vertexPosLocation, 2, gl.FLOAT, false, 0, 0);
        gl.enableVertexAttribArray(vertexPosLocation);
        gl.bindBuffer(gl.ARRAY_BUFFER, null);

        var vertexTexLocation = 4; // set with GLSL layout qualifier
        gl.bindBuffer(gl.ARRAY_BUFFER, vertexTexBuffer);
        gl.vertexAttribPointer(vertexTexLocation, 2, gl.FLOAT, false, 0, 0);
        gl.enableVertexAttribArray(vertexTexLocation);
        gl.bindBuffer(gl.ARRAY_BUFFER, null);

        gl.bindVertexArray(null);

        // -- Initialize samplers

        var samplers = new Array(Corners.MAX);
        for (var i = 0; i < Corners.MAX; ++i) {
            samplers[i] = gl.createSampler();
            gl.samplerParameteri(samplers[i], gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
            gl.samplerParameteri(samplers[i], gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
            gl.samplerParameteri(samplers[i], gl.TEXTURE_WRAP_R, gl.CLAMP_TO_EDGE);
        }

        // Min filter
        gl.samplerParameteri(samplers[Corners.TOP_LEFT], gl.TEXTURE_MIN_FILTER, gl.NEAREST);
        gl.samplerParameteri(samplers[Corners.TOP_RIGHT], gl.TEXTURE_MIN_FILTER, gl.LINEAR);
        gl.samplerParameteri(samplers[Corners.BOTTOM_RIGHT], gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST);
        gl.samplerParameteri(samplers[Corners.BOTTOM_LEFT], gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);

        // Mag filter
        gl.samplerParameteri(samplers[Corners.TOP_LEFT], gl.TEXTURE_MAG_FILTER, gl.NEAREST);
        gl.samplerParameteri(samplers[Corners.TOP_RIGHT], gl.TEXTURE_MAG_FILTER, gl.LINEAR);
        gl.samplerParameteri(samplers[Corners.BOTTOM_RIGHT], gl.TEXTURE_MAG_FILTER, gl.LINEAR);
        gl.samplerParameteri(samplers[Corners.BOTTOM_LEFT], gl.TEXTURE_MAG_FILTER, gl.LINEAR);

        // -- Load texture then render

        var imageUrl = '../assets/img/Di-3d.png';
        var texture;
        loadImage(imageUrl, function(image) {
            texture = gl.createTexture();
            gl.bindTexture(gl.TEXTURE_2D, texture);
            gl.texImage2D(
                gl.TEXTURE_2D,
                0, // Level of details
                gl.RGBA, // Format
                gl.RGBA,
                gl.UNSIGNED_BYTE, // Size of each channel
                image
            );
            gl.generateMipmap(gl.TEXTURE_2D);

            render();
        });

        function render() {
            // Clear color buffer
            gl.clearColor(0.0, 0.0, 0.0, 1.0);
            gl.clear(gl.COLOR_BUFFER_BIT);

            // Bind program
            gl.useProgram(program);

            var matrix = new Float32Array([
                1.0, 0.0, 0.0, 0.0,
                0.0, 1.0, 0.0, 0.0,
                0.0, 0.0, 1.0, 0.0,
                0.0, 0.0, 0.0, 1.0
            ]);
            gl.uniformMatrix4fv(uniformMvpLocation, false, matrix);
            gl.uniform1i(uniformDiffuseLocation, 0);

            gl.activeTexture(gl.TEXTURE0);
            gl.bindTexture(gl.TEXTURE_2D, texture);
            gl.bindVertexArray(vertexArray);

            // Bind samplers
            for (var i = 0; i < Corners.MAX; ++i) {
                gl.viewport(viewport[i].x, viewport[i].y, viewport[i].z, viewport[i].w);

                gl.bindSampler(0, samplers[i]);
                gl.drawArraysInstanced(gl.TRIANGLES, 0, 6, 1);
            }

            // Clean up
            gl.deleteBuffer(vertexPosBuffer);
            gl.deleteBuffer(vertexTexBuffer);
            for (var j = 0; j < samplers.length; ++j) {
                gl.deleteSampler(samplers[j]);
            }
            gl.deleteVertexArray(vertexArray);
            gl.deleteTexture(texture);
            gl.deleteProgram(program);
        }
    })();
    </script>
    <div id="highlightedLines"  style="display: none">#L176-L194</div>

</body>

</html>
